package de.tabuma.projectview;

import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import de.tabuma.pm.project.entity.*;
import de.tabuma.pm.project.logik.*;
import de.tabuma.ssoclient.logik.AttributeFacade;
import de.tabuma.ssoclient.entity.Attribute;

/**
 *
 * @author alefen
 */
public class javascriptview extends HttpServlet {

    private static final Logger log = Logger.getLogger(javascriptview.class.getName());

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String projectId = request.getParameter(constants.projectId);
            int projectIdInt = Integer.valueOf(projectId).intValue();
            Project project = de.tabuma.pm.project.logik.ProjectFacade.get(projectIdInt);

            String ssoId = request.getParameter(constants.ssoId);

            List<Attribute> aList = null;
            if(ssoId!=null && !ssoId.equals("null")) {
                aList = AttributeFacade.getList1(ssoId);
            }
            else {
                aList = new ArrayList<Attribute>();
            }
            out.println(de.tabuma.projectview.ribbon.JavaScriptFacade.getProjectString(request, project, aList));
        } 
        catch(Exception e) {
            String warning = "";
            warning += e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
